{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f63c57d9",
   "metadata": {},
   "source": [
    "## 1. APP 币本位合约行情-实时行情函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f27659f",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|get_bookTicker|获取一个产品的最优挂单|\n",
    "|get_bookTickers|获取全部产品的最优挂单（列表格式）|\n",
    "|get_bookTickersMap|获取全部产品的最优挂单 （字典格式）|\n",
    "|get_tickerPrice|获取一个产品的最新价格|\n",
    "|get_tickerPrices|获取全部产品的最新价格（列表格式）|\n",
    "|get_tickerPricesMap|获取全部产品的最新价格 (字典格式)|\n",
    "|get_depth|获取深度信息|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5525234",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "966773ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceCM\n",
    "from binance_interface.app.utils import eprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c7e7ff99",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceCM = BinanceCM(\n",
    "    key=key, secret=secret,\n",
    "    proxy_host=proxy_host\n",
    ")\n",
    "market = binanceCM.market"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1749cbb8",
   "metadata": {},
   "source": [
    "## 3. 获取一个产品的最优挂单 get_bookTicker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "043b0e71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'BTCUSD_PERP',\n",
      "           'pair': 'BTCUSD',\n",
      "           'bidPrice': '39977.9',\n",
      "           'bidQty': '638',\n",
      "           'askPrice': '39978.0',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "bookTicker_result = market.get_bookTicker(symbol='BTCUSD_PERP')\n",
    "eprint(bookTicker_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "417e6033",
   "metadata": {},
   "source": [
    "## 4. 获取全部产品的最优挂单（列表格式）get_bookTickers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "73e520e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'BTCUSD_PERP',\n",
      "           'pair': 'BTCUSD',\n",
      "           'bidPrice': '39977.9',\n",
      "           'bidQty': '2235',\n",
      "           'askPrice': '39978.0',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'bidPrice': '2208.17',\n",
      "           'bidQty': '40391',\n",
      "           'askPrice': '2208.18',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'LINKUSD_PERP',\n",
      "           'pair': 'LINKUSD',\n",
      "           'bidPrice': '13.736',\n",
      "           'bidQty': '22',\n",
      "           'askPrice': '13.737',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'BNBUSD_PERP',\n",
      "           'pair': 'BNBUSD',\n",
      "           'bidPrice': '293.600',\n",
      "           'bidQty': '109',\n",
      "           'askPrice': '293.610',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'TRXUSD_PERP',\n",
      "           'pair': 'TRXUSD',\n",
      "           'bidPrice': '0.11136',\n",
      "           'bidQty': '63',\n",
      "           'askPrice': '0.11137',\n",
      "           '...': '......'},\n",
      "          '......'],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数symbols默认为[]，表示全部产品\n",
    "bookTickers_result = market.get_bookTickers()\n",
    "eprint(bookTickers_result, data_length=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2150b5ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'BTCUSD_PERP',\n",
      "           'pair': 'BTCUSD',\n",
      "           'bidPrice': '39986.0',\n",
      "           'bidQty': '2162',\n",
      "           'askPrice': '39986.1',\n",
      "           '...': '......'},\n",
      "          {'symbol': 'ETHUSD_PERP',\n",
      "           'pair': 'ETHUSD',\n",
      "           'bidPrice': '2208.78',\n",
      "           'bidQty': '29637',\n",
      "           'askPrice': '2208.79',\n",
      "           '...': '......'}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 指定多个产品\n",
    "bookTickers_result = market.get_bookTickers(symbols=['BTCUSD_PERP', 'ETHUSD_PERP'])\n",
    "eprint(bookTickers_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e985ca2a",
   "metadata": {},
   "source": [
    "## 5. 获取全部产品的最优挂单 （字典格式）get_bookTickersMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d66378c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'BTCUSD_PERP': {'symbol': 'BTCUSD_PERP',\n",
      "                          'pair': 'BTCUSD',\n",
      "                          'bidPrice': '39986.0',\n",
      "                          'bidQty': '6253',\n",
      "                          'askPrice': '39986.1',\n",
      "                          '...': '......'},\n",
      "          'ETHUSD_PERP': {'symbol': 'ETHUSD_PERP',\n",
      "                          'pair': 'ETHUSD',\n",
      "                          'bidPrice': '2208.62',\n",
      "                          'bidQty': '58237',\n",
      "                          'askPrice': '2208.63',\n",
      "                          '...': '......'},\n",
      "          'LINKUSD_PERP': {'symbol': 'LINKUSD_PERP',\n",
      "                           'pair': 'LINKUSD',\n",
      "                           'bidPrice': '13.739',\n",
      "                           'bidQty': '11',\n",
      "                           'askPrice': '13.742',\n",
      "                           '...': '......'},\n",
      "          'BNBUSD_PERP': {'symbol': 'BNBUSD_PERP',\n",
      "                          'pair': 'BNBUSD',\n",
      "                          'bidPrice': '293.710',\n",
      "                          'bidQty': '156',\n",
      "                          'askPrice': '293.720',\n",
      "                          '...': '......'},\n",
      "          'TRXUSD_PERP': {'symbol': 'TRXUSD_PERP',\n",
      "                          'pair': 'TRXUSD',\n",
      "                          'bidPrice': '0.11138',\n",
      "                          'bidQty': '82',\n",
      "                          'askPrice': '0.11139',\n",
      "                          '...': '......'},\n",
      "          'DOTUSD_PERP': {'symbol': 'DOTUSD_PERP',\n",
      "                          'pair': 'DOTUSD',\n",
      "                          'bidPrice': '6.439',\n",
      "                          'bidQty': '30',\n",
      "                          'askPrice': '6.440',\n",
      "                          '...': '......'},\n",
      "          '...': '......'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数symbols默认为[]，表示全部产品\n",
    "bookTickersMap_result = market.get_bookTickersMap()\n",
    "eprint(bookTickersMap_result, data_length=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "76093cf3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'BTCUSD_PERP': {'symbol': 'BTCUSD_PERP',\n",
      "                          'pair': 'BTCUSD',\n",
      "                          'bidPrice': '39986.1',\n",
      "                          'bidQty': '6140',\n",
      "                          'askPrice': '39986.2',\n",
      "                          '...': '......'},\n",
      "          'ETHUSD_PERP': {'symbol': 'ETHUSD_PERP',\n",
      "                          'pair': 'ETHUSD',\n",
      "                          'bidPrice': '2208.87',\n",
      "                          'bidQty': '26036',\n",
      "                          'askPrice': '2208.88',\n",
      "                          '...': '......'}},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 指定多个产品\n",
    "bookTickersMap_result = market.get_bookTickersMap(symbols=['BTCUSD_PERP', 'ETHUSD_PERP'])\n",
    "eprint(bookTickersMap_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cb262a2",
   "metadata": {},
   "source": [
    "## 6. 获取一个产品的最新价格 get_tickerPrice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e3d25d1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': [{'symbol': 'BTCUSD_PERP', 'ps': 'BTCUSD', 'price': '39986.1', 'time': 1706192790208}], 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "tickerPrice_result = market.get_tickerPrice(symbol='BTCUSD_PERP')\n",
    "eprint(tickerPrice_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b959dbb3",
   "metadata": {},
   "source": [
    "## 7. 获取全部产品的最新价格（列表格式） get_tickerPrices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "910ea4ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'ETHUSD_240329', 'ps': 'ETHUSD', 'price': '2244.30', 'time': 1706192782896},\n",
      "          {'symbol': 'BCHUSD_PERP', 'ps': 'BCHUSD', 'price': '237.21', 'time': 1706192788737},\n",
      "          {'symbol': 'GMTUSD_PERP', 'ps': 'GMTUSD', 'price': '0.25630', 'time': 1706192765222},\n",
      "          {'symbol': 'LTCUSD_240329', 'ps': 'LTCUSD', 'price': '66.78', 'time': 1706192787556},\n",
      "          {'symbol': 'RUNEUSD_PERP', 'ps': 'RUNEUSD', 'price': '4.0290', 'time': 1706192785876},\n",
      "          '......'],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数symbols默认为[]，表示全部产品\n",
    "tickerPrices_result = market.get_tickerPrices()\n",
    "eprint(tickerPrices_result, data_length=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2d6b7134",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': [{'symbol': 'ETHUSD_PERP', 'ps': 'ETHUSD', 'price': '2208.28', 'time': 1706192809240},\n",
      "          {'symbol': 'BTCUSD_PERP', 'ps': 'BTCUSD', 'price': '39983.1', 'time': 1706192810807}],\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 指定多个产品\n",
    "tickerPrices_result = market.get_tickerPrices(symbols=['BTCUSD_PERP', 'ETHUSD_PERP'])\n",
    "eprint(tickerPrices_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bc2f1f3",
   "metadata": {},
   "source": [
    "## 8. 获取全部产品的最新价格 (字典格式) get_tickerPricesMap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a8957f6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'ETHUSD_240329': {'symbol': 'ETHUSD_240329', 'ps': 'ETHUSD', 'price': '2243.20', 'time': 1706192820919},\n",
      "          'BCHUSD_PERP': {'symbol': 'BCHUSD_PERP', 'ps': 'BCHUSD', 'price': '237.25', 'time': 1706192820879},\n",
      "          'GMTUSD_PERP': {'symbol': 'GMTUSD_PERP', 'ps': 'GMTUSD', 'price': '0.25620', 'time': 1706192812086},\n",
      "          'LTCUSD_240329': {'symbol': 'LTCUSD_240329', 'ps': 'LTCUSD', 'price': '66.78', 'time': 1706192787556},\n",
      "          'RUNEUSD_PERP': {'symbol': 'RUNEUSD_PERP', 'ps': 'RUNEUSD', 'price': '4.0270', 'time': 1706192821010},\n",
      "          'ROSEUSD_PERP': {'symbol': 'ROSEUSD_PERP', 'ps': 'ROSEUSD', 'price': '0.09782', 'time': 1706192812608},\n",
      "          '...': '......'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 参数symbols默认为[]，表示全部产品\n",
    "tickerPricesMap_result = market.get_tickerPricesMap()\n",
    "eprint(tickerPricesMap_result, data_length=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5590a01e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'ETHUSD_PERP': {'symbol': 'ETHUSD_PERP', 'ps': 'ETHUSD', 'price': '2208.20', 'time': 1706192826180},\n",
      "          'BTCUSD_PERP': {'symbol': 'BTCUSD_PERP', 'ps': 'BTCUSD', 'price': '39979.0', 'time': 1706192826071}},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 指定多个产品\n",
    "tickerPricesMap_result = market.get_tickerPricesMap(symbols=['BTCUSD_PERP', 'ETHUSD_PERP'])\n",
    "eprint(tickerPricesMap_result, data_length=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cbdefec",
   "metadata": {},
   "source": [
    "## 9. 获取深度信息 get_depth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bbff3cbe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': {'lastUpdateId': 843861312615,\n",
      "          'E': 1706192837094,\n",
      "          'T': 1706192837085,\n",
      "          'symbol': 'BTCUSD_PERP',\n",
      "          'pair': 'BTCUSD',\n",
      "          'bids': [['39978.0', '518'],\n",
      "                   ['39977.9', '1'],\n",
      "                   ['39977.0', '56'],\n",
      "                   ['39976.4', '327'],\n",
      "                   ['39976.3', '500'],\n",
      "                   '......'],\n",
      "          '...': '......'},\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "depth_result = market.get_depth(symbol='BTCUSD_PERP')\n",
    "eprint(depth_result, data_length=5)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
